﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InSys.Documents
{
    public partial class FormDocumentsList : Form
    {
        private AccessUserLoginInfo info;
        private MapTestLinqDataContext db = new MapTestLinqDataContext(Properties.Settings.Default.testlinqConnectionString);
        public FormDocumentsList()
        {
            InitializeComponent();
            info = new AccessUserLoginInfo();
            var query = from item in db.departments select item;
            departmentBindingSource.DataSource = query;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Validate();
            db.SubmitChanges();
        }

        private void nameComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearTableDataBinding();
            if (null == nameComboBox.SelectedValue)
            {
                personBindingSource.DataSource = null;
                return;
            }


            IEnumerable<person> Person = info.getListPersonsDepartment((int)nameComboBox.SelectedValue, Program.gWorkDate);
            personBindingSource.DataSource = Person;
        }

        private void nameComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearTableDataBinding();
            int id_department = (null == nameComboBox.SelectedValue) ? 0 : (int)nameComboBox.SelectedValue;
            int id_person = (null == nameComboBox1.SelectedValue) ? 0 : (int)nameComboBox1.SelectedValue;
            if (null == nameComboBox1.SelectedValue)
            {
                return;
            }

            var query = from item in db.person_documents where item.id_person == id_person select item.document;
            documentBindingSource.DataSource = query;
        }

        private void ClearTableDataBinding()
        {
            documentBindingSource.DataSource = null;
        }

    }
}
